class Solution{
    public:
        int lengthOfLongestSubstring(string s) {
            int *flag = new int[256], *pos = new int[256];
            int l = 0, h = -1, maxLen = 0;
            memset(flag, 0, 256*sizeof(int));
            memset(pos, 0, 256*sizeof(int));
            for(unsigned i=0; i<s.length(); i++) {
                unsigned k = s[i];
                if(flag[k]) {
                    for(int j=l; j<pos[k]; j++) flag[(unsigned)s[j]] = 0;
                    l = pos[k]+1;
                    h = i;
                    pos[k] = i;
                } else {
                    h++;
                    flag[k] = 1;
                    pos[k] = i;
                }
                if(h-l+1>maxLen) maxLen = h-l+1;
            }
            delete[] flag;
            delete[] pos;
            return maxLen;
        }
};
